package cn.satoken.controller;

import cn.dev33.satoken.util.SaResult;
import cn.satoken.entity.CrmDeptment;
import cn.satoken.mapper.DeptmentMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import static cn.satoken.common.ValueValid.ensureColumnValueValid;
import static cn.satoken.constant.StatusConstant.*;

/**
 * <h3>saToken</h3>
 * <p></p>
 *
 * @author gdw
 * @since 2025-04-18 10:41:04
 */
@RestController
@RequestMapping("/dept/")
@RequiredArgsConstructor
public class DeptmentController {
    private final DeptmentMapper deptmentMapper;
    //查询所有部门信息
    @GetMapping("selectAll")
    public SaResult selectAll() {
        return SaResult.data(deptmentMapper.selectList(null));
    }
    //修改信息
    @PostMapping("update")
    public SaResult update(@Valid @RequestBody CrmDeptment crmDeptment) {
        if(!ensureColumnValueValid(crmDeptment.getId(), CrmDeptment::getId,deptmentMapper::selectOne)) return SaResult.error(SELECT_ID_DATA_ERRO);
        deptmentMapper.updateById(crmDeptment);
        return SaResult.ok(UPDATE_SUCCESS);
    }
    //删除信息
    @GetMapping("delete/{id}")
    public SaResult delete(@PathVariable Long id) {
        if(!ensureColumnValueValid(id, CrmDeptment::getId,deptmentMapper::selectOne)) return SaResult.error(SELECT_ID_DATA_ERRO);
        deptmentMapper.deleteById(id);
        return SaResult.ok(DELETE_SUCCESS);
    }
    //保存部门
    @PostMapping("save")
    public SaResult save(@RequestBody CrmDeptment crmDeptment) {
        deptmentMapper.insert(crmDeptment);
        return SaResult.ok(SAVE_SUCCESS);
    }

}
